package com.hunter.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hunter.lease.common.constant.RedisConstant;
import com.hunter.lease.common.exception.LeaseException;
import com.hunter.lease.common.result.ResultCodeEnum;
import com.hunter.lease.model.entity.*;
import com.hunter.lease.model.enums.ItemType;
import com.hunter.lease.web.admin.mapper.*;
import com.hunter.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hunter.lease.web.admin.vo.attr.AttrValueVo;
import com.hunter.lease.web.admin.vo.graph.GraphVo;
import com.hunter.lease.web.admin.vo.room.RoomDetailVo;
import com.hunter.lease.web.admin.vo.room.RoomItemVo;
import com.hunter.lease.web.admin.vo.room.RoomQueryVo;
import com.hunter.lease.web.admin.vo.room.RoomSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override

    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {

        boolean isUpdate = roomSubmitVo.getId() != null;
        super.saveOrUpdate(roomSubmitVo);

        // 判断是否为更新操作
        if (!isUpdate) {

            // 1. 删除图片列表 graphVoList

            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
            graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoService.remove(graphQueryWrapper);

            // 2. 删除属性信息列表 roomAttrValueInfoIds

            LambdaQueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueQueryWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueQueryWrapper);

            // 3. 删除配套信息列表 roomFacilityInfoIds

            LambdaQueryWrapper<RoomFacility> roomFacilityQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityQueryWrapper);

            // 4. 删除标签信息列表 roomLabelInfoIds

            LambdaQueryWrapper<RoomLabel> roomLabelQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(roomLabelQueryWrapper);

            // 5. 删除支付方式列表 roomPaymentsTypeIds
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);


            // 6. 删除可选租期列表 roomLeaseTermIds

            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermQueryWrapper);

            // 7.删除缓存

            String key = RedisConstant.APP_LOGIN_PREFIX+roomSubmitVo.getId();

            redisTemplate.delete(key);

        }

        // 1. 更新图片列表 graphVoList

        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

        // 2. 更新属性信息列表 roomAttrValueInfoIds

        List<Long> attrValueIdList = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIdList)) {
            ArrayList<RoomAttrValue> attrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIdList) {
                RoomAttrValue roomAttrValue = new RoomAttrValue();
                roomAttrValue.setRoomId(roomSubmitVo.getId());
                roomAttrValue.setAttrValueId(attrValueId);
                attrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(attrValueList);
        }

        // 3. 更新配套信息列表 roomFacilityInfoIds

        List<Long> facilityInfoIdList = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIdList)) {
            ArrayList<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIdList) {
                RoomFacility roomFacility = new RoomFacility();
                roomFacility.setRoomId(roomSubmitVo.getId());
                roomFacility.setFacilityId(facilityInfoId);
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }

        // 4. 更新标签信息列表 roomLabelInfoIds

        List<Long> labelInfoIdList = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIdList)) {
            ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIdList) {
                RoomLabel roomLabel = new RoomLabel();
                roomLabel.setRoomId(roomSubmitVo.getId());
                roomLabel.setLabelId(labelInfoId);
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }

        // 5. 更新支付方式列表 roomPaymentsTypeIds

        List<Long> paymentTypeIdList = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIdList)) {
            ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIdList) {
                RoomPaymentType roomPaymentType = new RoomPaymentType();
                roomPaymentType.setRoomId(roomSubmitVo.getId());
                roomPaymentType.setPaymentTypeId(paymentTypeId);
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        // 6. 更新可选租期列表 roomLeaseTermIds

        List<Long> leaseTermIdList = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIdList)) {
            ArrayList<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
            for (Long leaseTermId : leaseTermIdList) {
                RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
                roomLeaseTerm.setRoomId(roomSubmitVo.getId());
                roomLeaseTerm.setLeaseTermId(leaseTermId);
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }

    }

    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {

        // 1.查询房间信息

        RoomInfo roomInfo = roomInfoMapper.selectById(id);

        // 2.查询所属公寓信息

        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);

        // 3.查询 graphInfoList

        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);

        // 4.查询 attrValueList

        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);

        // 5.查询 facilityInfoList

        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);

        // 6.查询 labelInfoList

        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);

        // 7.查询 paymentTypeList

        List<PaymentType> paymentTypeList = paymentTypeMapper.selectByRoomId(id);

        // 8.查询 leaseTermList

        List<LeaseTerm> leaseTermList = leaseTermMapper.selectByRoomId(id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }

    @Override
    public void removeByRoomId(Long id) {

        // 删除 RoomInfo

        super.removeById(id);

        // 删除 graphVoList

        LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphInfoQueryWrapper);

        // 删除 attrValueVoList

        LambdaQueryWrapper<RoomAttrValue> attrValueVoQueryWrapper = new LambdaQueryWrapper<>();
        attrValueVoQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(attrValueVoQueryWrapper);

        // 删除 facilityInfoList

        LambdaQueryWrapper<RoomFacility> roomFacilityQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(roomFacilityQueryWrapper);

        // 删除 labelInfoList

        LambdaQueryWrapper<RoomLabel> roomLabelQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(roomLabelQueryWrapper);

        // 删除 paymentTypeList

        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);

        // 删除 leaseTermList

        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(roomLeaseTermQueryWrapper);

        // 删除缓存

        String key = RedisConstant.APP_LOGIN_PREFIX+id;

        redisTemplate.delete(key);
    }

    @Override
    public void checkApartmentId(Long id) {

        // 公寓ID 校验

        if (id == null || id <= 0) {
            throw new LeaseException(ResultCodeEnum.APARTMENT_ID_ILLEGAL);
        }

        // 公寓信息校验

        ApartmentInfo apartmentInfo = apartmentInfoService.getById(id);
        if (apartmentInfo == null) {
            throw new LeaseException(ResultCodeEnum.APARTMENT_NULL);
        }

        // 公寓下房间信息校验

        LambdaQueryWrapper<RoomInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomInfo::getApartmentId, id);
        long count = this.count(queryWrapper);
        if (count == 0) {
            throw new LeaseException(ResultCodeEnum.APARTMENT_ROOM_NULL);
        }

    }


}




